<?php

class Application_Model_SimpleDb extends Zend_Service_Amazon_SimpleDb {
    protected $domain = 'c2dm';

    public function  __construct(){
        // Load Amazon Configuration
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/amazon.ini', APPLICATION_ENV);
        $access_key = $config->aws->account->access_key;
        $secret_key = $config->aws->account->secret_key;
        parent::__construct($access_key, $secret_key);
        // Load HTTP Configuration
        $httpconfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/http.ini', APPLICATION_ENV);
        $client = new Zend_Http_Client(null, $httpconfig);
        $this->setHttpClient($client);
    }

    public function getMetaData(){
        $response = $this->domainMetadata($this->domain);
        return $response;
    }


    public function getAll(){
        $sql = "Select * From ".$this->domain;
        try {
            $response = $this->select($sql);
        }
        catch (Exception $e){
            $response = array(
                "error" => true,
                "msg"	=> $e->getMessage()
            );
        }
        return $response;
    }


    public function get($item_id){
        $response = $this->getAttributes($this->domain, $item_id);
        return $response;
    }

    public function insert($user_id, $device_id, $registration_id, $device_type, $timestamp=null){
        // Build Attributes List
        $attributes = array(
            new Zend_Service_Amazon_SimpleDb_Attribute($device_id, "user_id", $user_id),
            new Zend_Service_Amazon_SimpleDb_Attribute($device_id, "registration_id", $registration_id),
            new Zend_Service_Amazon_SimpleDb_Attribute($device_id, "device_type", $device_type),
            new Zend_Service_Amazon_SimpleDb_Attribute($device_id, "timestamp", time()),
        );
        try {
            $this->putAttributes($this->domain, $device_id, $attributes);
            $response = array(
                "success" => true
            );
        } catch(Exception $e) {
            $response = array(
                "error" => true,
                "msg"	=> $e->getMessage()
            );
        }
        return $response;
    }

    public function deleteItem($item_id){
        try {
            $response = $this->deleteAttributes($this->domain, $item_id);
            $response = array(
                "success" => true
            );
        } catch(Exception $e) {
            $response = array(
                "error" => true,
                "msg"	=> $e->getMessage()
            );
        }
        return $response;
    }

    public function batchDelete($items){
        $params               = array();
        $params['Action']     = 'BatchDeleteAttributes';
        $params['DomainName'] = $this->domain;
        $itemIndex = 0;
        foreach ($items as $name => $attributes) {
                $params['Item.' . $itemIndex . '.ItemName'] = $name;
                $attributeIndex = 0;
                foreach ($attributes as $attribute) {
                        // attribute value cannot be array, so when several items are passed
                        // they are treated as separate values with the same attribute name
                        foreach($attribute->getValues() as $value) {
                                $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Name'] = $attribute->getName();
                                $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Value'] = $value;
                                if (isset($replace[$name])
                                        && isset($replace[$name][$attribute->getName()])
                                        && $replace[$name][$attribute->getName()]
                                ) {
                                        $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Replace'] = 'true';
                                }
                                $attributeIndex++;
                        }
                }
                $itemIndex++;
        }
        $response = $this->_sendRequest($params);
    }

}

?>
